<template>
  <div>
    <div class="box-border p-12px">
      <div>
        <SubTitle :title="descInfo.baseTitle" />

        <el-descriptions :column="descInfo.column">
          <el-descriptions-item label="任务名称：">
            {{ descInfo.data.fileName || descInfo.data.name }}审核
          </el-descriptions-item>
          <el-descriptions-item
            :label="item.label"
            v-for="(item, index) in descInfo.columns"
            :key="index"
          >
            <span v-if="item.key == 'source'">
              {{ sourceMap.has(descInfo.data.source) ? sourceMap.get(descInfo.data.source) : '--' }}
            </span>
            <span v-if="item.key == 'fileFingerprint'">
              <el-tooltip placement="top" :content="descInfo.data.md5?.toString() || '无'">
                <span>MD5,</span>
              </el-tooltip>
              <el-tooltip placement="top" :content="descInfo.data.kfs?.toString() || '无'">
                <span>KFS,</span>
              </el-tooltip>
              <el-tooltip placement="top" :content="descInfo.data.pfs?.toString() || '无'">
                <span>PFS,</span>
              </el-tooltip>
              <el-tooltip placement="top" :content="descInfo.data.psa?.toString() || '无'">
                <span>PSA</span>
              </el-tooltip>
            </span>
            <span v-else>
              {{ descInfo.data[item.key] || '--' }}
            </span>
          </el-descriptions-item>
        </el-descriptions>
      </div>
      <div class="mt-32px" v-if="props.params.processType !== 'file'">
        <SubTitle title="分类状态" />
        <el-table :data="classTable" style="width: 100%">
          <el-table-column label="" prop="title" />
          <el-table-column label="总数">
            <template #default="scope">
              <div>{{ scope.row.total }} </div>
            </template>
          </el-table-column>
          <el-table-column label="已分类">
            <template #default="scope">
              <div>{{ scope.row.classified }} </div>
            </template>
          </el-table-column>
          <el-table-column label="未分类">
            <template #default="scope">
              <div>{{ scope.row.unclassified }} </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="mt-32px" v-if="props.params.processType !== 'file'">
        <SubTitle title="分级状态" />
        <el-table :data="levelTable" style="width: 100%">
          <el-table-column label="" prop="title" />
          <el-table-column label="总数">
            <template #default="scope">
              <div>{{ scope.row.total }} </div>
            </template>
          </el-table-column>
          <el-table-column label="已分级">
            <template #default="scope">
              <div>{{ scope.row.classified }} </div>
            </template>
          </el-table-column>
          <el-table-column label="未分级">
            <template #default="scope">
              <div>{{ scope.row.unclassified }} </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="mt-32px" v-if="props.params.processType === 'file'">
        <SubTitle title="分类分级状态" />
        <el-descriptions column="1" class="mt-16px">
          <el-descriptions-item
            v-for="(columnItem, columnIndex) in columnList"
            :key="columnIndex"
            :label="columnItem.name"
          >
            <span v-if="columnItem.index == '1'">
              {{ descInfo.data.handClassificationName }}
            </span>
            <span v-else-if="columnItem.index == '2'">
              {{ descInfo.data.handLevelName }}
            </span>
            <span v-else>
              {{ descInfo.data[`institutionFiled${columnItem.index}`] || '--' }}
            </span>
          </el-descriptions-item>
          <el-descriptions-item
            v-for="(extendItem, extendIndex) in extendList"
            :key="extendIndex"
            :label="extendItem.name"
          >
            <span v-if="extendItem.index == '3'">
              {{ descInfo.data.deadlineLevelName }}
            </span>
            <span v-else>
              {{ descInfo.data[`field${extendItem.index}`] || '--' }}
            </span>
          </el-descriptions-item>
        </el-descriptions>
      </div>
      <!-- <div class="mt-32px">
        <SubTitle title="流程" />
        <el-form ref="formRef" :rules="rules" :model="form" label-width="100px" class="mt-12px">
          <el-form-item label="流程选择" required prop="processDefinitionKey">
            <el-select v-model="form.processDefinitionKey" placeholder="请选择" disabled>
              <el-option
                v-for="item in processList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-form>
      </div> -->
    </div>
    <div>
      <el-button type="primary" @click="confirm" :loading="confirmLoading">确定</el-button>
      <el-button @click="closeOutDrawer" :loading="confirmLoading">取消</el-button>
    </div>
  </div>
</template>
<script lang="ts" setup>
import { sourceMap } from '@/utils/dict'
import SubTitle from '@/components/SubTitle/index.vue'
import * as assuranceApi from '@/api/dataStandards/assurance/index'
import { infoPage } from '@/api/dataStandards/info/index'
import { getProcessDefinitionOne } from '@/api/bpm/definition'
import {
  assetSourceAudit,
  fileAudit,
  assetLevelCountDB,
  assetLevelCountFile
} from '@/api/assetList'
const message = useMessage() // 消息弹窗
const confirmLoading = ref(false)
const props = defineProps({
  params: {
    type: Object,
    default: () => {}
  },
  closeOutDrawer: Function
})

const formRef = ref()
const form = ref({
  processDefinitionKey: '',
  id: ''
})
const rules = {
  processDefinitionKey: [{ required: true, message: '流程不能为空', trigger: 'blur' }]
}

const classTable = ref([
  {
    total: 1000,
    classified: 800,
    unclassified: 200
  }
])
const levelTable = ref([
  {
    total: 1000,
    classified: 800,
    unclassified: 200
  }
])

const columnList = ref([])
const extendList = ref([])

const descInfo = reactive({
  column: 1,
  columns: [
    { label: '数据源名称：', key: 'name' },
    { label: '资产地址：', key: 'ip' },
    { label: '资产类型：', key: 'assetTypeName' },
    { label: '数据源来源：', key: 'source' },
    { label: '归属机构：', key: 'deptName' },
    { label: '归属系统：', key: 'sysName' }
  ],
  data: {},
  baseTitle: '数据库基本信息'
})

// 获取分类分级数据
const getCount = async () => {
  if (props.params.id && props.params.datasourceType == '1') {
    const data = await assetLevelCountDB({ id: props.params.id })
    console.log('data', data)
    levelTable.value = [
      {
        title: '数据表',
        total: data[0].totalCount,
        classified: data[0].level,
        unclassified: data[0].unLevel
      },
      {
        title: '数据字段',
        total: data[1].totalCount,
        classified: data[1].level,
        unclassified: data[1].unLevel
      }
    ]
    classTable.value = [
      {
        title: '数据表',
        total: data[0].totalCount,
        classified: data[0].classification,
        unclassified: data[0].unClassification
      },
      {
        title: '数据字段',
        total: data[1].totalCount,
        classified: data[1].classification,
        unclassified: data[1].unClassification
      }
    ]
  } else if (props.params.id && props.params.datasourceType == '2') {
    // 文件服务器
    const data = await assetLevelCountFile({ id: props.params.id })
    console.log('data', data)
    levelTable.value = [
      {
        title: '分级',
        total: data.totalCount,
        classified: data.level,
        unclassified: data.unLevel
      }
    ]
    classTable.value = [
      {
        title: '分类',
        total: data.totalCount,
        classified: data.classification,
        unclassified: data.unClassification
      }
    ]
  }
}

const confirm = async () => {
  confirmLoading.value = true
  try {
    // processDefinitionKey取流程id第一个冒号前的字符串
    const firstColonIndex = form.value.processDefinitionKey.indexOf(':')
    const parsedString = form.value.processDefinitionKey.slice(0, firstColonIndex)
    const apiInterface = props.params.processType === 'file' ? fileAudit : assetSourceAudit
    const data = await apiInterface({
      id: form.value.id,
      processDefinitionKey: parsedString
    })
    console.log('data', data)
    if (data) {
      message.success('操作成功')
      props.params.callBack()
      props.closeOutDrawer()
    }
  } catch {
  } finally {
    confirmLoading.value = false
  }
}

const processList = ref([])
const getProcessList = async () => {
  try {
    processList.value = await getProcessDefinitionOne({
      assetId: props.params.id,
      assetType: props.params.processType === 'file' ? 'file' : 'db',
      deptId: props.params.deptId,
      sysId: props.params.sysId
    })
    if (processList.value.length) {
      form.value.processDefinitionKey = processList.value[0].id
    }
  } finally {
  }
}

// 获取制度信息字段列表
const getAssuranceList = async () => {
  const data = await assuranceApi.assurancePage({
    dataStandardId: props.params.template,
    display: 1,
    page: 1,
    pageSize: 20
  })
  if (data.length > 0) {
    columnList.value = data
    console.log('columnList', columnList.value)
  } else {
    columnList.value = []
  }
}

// 获取模板扩展字段列表
const getInfoList = async () => {
  const data = await infoPage({
    page: 1,
    pageSize: 20,
    dataStandardId: props.params.template,
    display: '1'
  })
  extendList.value = data.map((item) => {
    return {
      ...item,
      presetContentMap: item.presetContent ? JSON.parse(item.presetContent) : []
    }
  })
}

onMounted(() => {
  getCount()
  getProcessList()
  getAssuranceList()
  getInfoList()
  descInfo.data = props.params
  console.log('descInfo', descInfo)
  if (props.params.type === '自动') {
    descInfo.data.handClassificationName = props.params.classificationName
    descInfo.data.handLevelName = String(props.params.levelName)
  }
  form.value.id = props.params.id
  if (props.params.processType === 'fileServer') {
    descInfo.baseTitle = '文件服务器基本信息'
  } else if (props.params.processType === 'file') {
    descInfo.baseTitle = '文件基本信息'
    descInfo.columns = [
      { label: '文件名称：', key: 'fileName' },
      { label: '文件类型：', key: 'fileType' },
      { label: '文件指纹：', key: 'fileFingerprint' },
      // { label: '敏感等级：', key: 'level' },
      { label: '归属机构：', key: 'deptName' },
      { label: '归属系统：', key: 'sysName' },
      { label: '数据标准：', key: 'templateName' }
      // { label: '分类分级方式：', key: 'classificationWay' }
    ]
  }
})
</script>

<style scoped lang="scss">
:deep(.el-table .el-form-item--default) {
  margin-bottom: 0 !important;
}
</style>
